<?php

require_once(drupal_get_path('module','gc_filters').'/interfaces/IGcPane.interface.php');

class GcProducerPane implements IGcPane {
  
  
	private function getActiveFilterValuesCacheEnabled($gid) {
	   $cache_key = 'pane_in_group_values_'.$gid.'_gcf_producer';
	   $cache = $cache = cache_get($cache_key,'cache_gc_filters');
     $values = array();

     // czy mamy w keszu ?
     if(!$cache->data) {
	   	$values = $this->getActiveFilterValuesRoutine($gid);
	   	cache_set($cache_key,$values,'cache_gc_filters');
	   } else {
	   	$values = $cache->data;
	   }
	   
	   return $values;
	}
	
	private function getActiveFilterValuesRoutine($gid) {
		$contentNids = _group_get_content_nids(node_load($gid));
    

    $producers = array();
    $sql = db_query("SELECT DISTINCT field_producent_value FROM {node} n 
             RIGHT JOIN {content_type_product} p ON n.nid = p.nid 
             WHERE p.field_producent_value is not NULL AND p.nid IN (". db_placeholders($contentNids, 'int').")",
             $contentNids);
    
    /*while($res = db_fetch_object($sql)) {
      $producers[$res->field_producent_value] = $res->field_producent_value;
    }*/

    return $producers;
	}
	
  private function getActiveFilterValues($gid) {
    return $this->getActiveFilterValuesCacheEnabled($gid);
  }
   
    
   /**
   * funkcja do tworzenia elementu formularza filtracji, odpowiedzialnego za
   * filtrowanie po producencie
   * 
   * @param int $gid
   *  id grupy dla której panel ma zostać wygenerowany 
   * @return Array 
   */
  
  public function buildPane($gid) {
    $options = array();
    $options[0] = t('none selected');
    $options = array_merge($options,$this->getActiveFilterValues($gid));
  
     
    $default = 0;
    if(isset($_GET['gcfProd'])) {
      $default = ($_GET['gcfProd']);
    }
    $form['producer_value'] = array(
      '#title' => t('select manufacturer'),
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $default  
    );
    return $form;
  }
 }